/*
*    Phantom v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
// OptionsDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PhantomAgent.h"
#include "OptionsDlg.h"
#include ".\optionsdlg.h"


// COptionsDlg dialog

IMPLEMENT_DYNAMIC(COptionsDlg, CDialog)
COptionsDlg::COptionsDlg(CWnd* pParent /*=NULL*/)
	: CDialog(COptionsDlg::IDD, pParent)
	, pW(NULL)
{
	pW = (CPhantomAgentDlg*)pParent;
}

COptionsDlg::~COptionsDlg()
{
}

void COptionsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(COptionsDlg, CDialog)
	//ON_BN_CLICKED(IDC_OPT_USE_SSL, OnBnClickedOptUseSsl)
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
	ON_BN_CLICKED(IDC_OPT_SAVE_LOG, OnBnClickedOptSaveLog)
  //ON_BN_CLICKED(IDC_OPT_SAVE_LOG2, OnBnClickedOptSaveLog2)
END_MESSAGE_MAP()


// COptionsDlg message handlers

//void COptionsDlg::OnBnClickedOptUseSsl()
//{
//	CButton* aChk = (CButton*)GetDlgItem(IDC_OPT_USE_SSL);
//	CEdit* aEdt = (CEdit*)GetDlgItem(IDC_OPT_SSL_KEY);
//
//	if(aChk->GetCheck() == BST_CHECKED){
//		aEdt->EnableWindow(TRUE);
//	}else{
//		aEdt->EnableWindow(FALSE);
//	}
//}

BOOL COptionsDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	InitializeOptions();
	//OnBnClickedOptUseSsl();
	OnBnClickedOptSaveLog();

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

int COptionsDlg::InitializeOptions(void)
{
	if(!pW) return 0;
	String sVal;

	if(pW->options.GetOption(OPT_LISTEN_PORT, sVal)){
		SetDlgItemText(IDC_OPT_LISTEN_PORT, sVal.Str());
	}

	if(pW->options.GetOption(OPT_REG_KEY, sVal)){
		SetDlgItemText(IDC_OPT_REG_KEY, sVal.Str());
	}


	//if(pW->options.GetOption(OPT_MIN_AGENT_PORT, sVal)){
	//	SetDlgItemText(IDC_OPT_MIN_PORT, sVal.Str());
	//}
	//if(pW->options.GetOption(OPT_MAX_AGENT_PORT, sVal)){
	//	SetDlgItemText(IDC_OPT_MAX_PORT, sVal.Str());
	//}
	//if(pW->options.GetOption(OPT_MAX_CONNECTIONS, sVal)){
	//	SetDlgItemText(IDC_OPT_MAX_CONN, sVal.Str());
	//}
	//if(pW->options.GetOption(OPT_USE_SSL, sVal)){
	//	CButton* aChk = (CButton*)GetDlgItem(IDC_OPT_USE_SSL);
	//	if(sVal == "1"){
	//		aChk->SetCheck(BST_CHECKED);
	//	}else{
	//		aChk->SetCheck(BST_UNCHECKED);
	//	}
	//}
  if(pW->options.GetOption(OPT_HIDE_AGENT, sVal)){
		CButton* aChk = (CButton*)GetDlgItem(IDC_OPT_HIDE_AGENT);
		if(sVal == "1"){
			aChk->SetCheck(BST_CHECKED);
		}else{
			aChk->SetCheck(BST_UNCHECKED);
		}
	}
	//if(pW->options.GetOption(OPT_SSL_KEY, sVal)){
	//	SetDlgItemText(IDC_OPT_SSL_KEY, sVal.Str());
	//}

	if(pW->options.GetOption(OPT_SAVE_LOG, sVal)){
		CButton* aChk = (CButton*)GetDlgItem(IDC_OPT_SAVE_LOG);
		if(sVal == "1"){
			aChk->SetCheck(BST_CHECKED);
		}else{
			aChk->SetCheck(BST_UNCHECKED);
		}
	}

	if(pW->options.GetOption(OPT_SAVE_LOG_FILE, sVal)){
		SetDlgItemText(IDC_OPT_EVT_LOG, sVal.Str());
	}

	return 0;
}

int COptionsDlg::SaveOptions(void)
{
	if(!pW) return 0;
	CString sVal;
	GetDlgItemText(IDC_OPT_LISTEN_PORT, sVal);
	pW->options.SetOption(OPT_LISTEN_PORT, String((LPCTSTR)sVal));
	//GetDlgItemText(IDC_OPT_MIN_PORT, sVal);
	//pW->options.SetOption(OPT_MIN_AGENT_PORT, String((LPCTSTR)sVal));
	//GetDlgItemText(IDC_OPT_MAX_PORT, sVal);
	//pW->options.SetOption(OPT_MAX_AGENT_PORT, String((LPCTSTR)sVal));
	//GetDlgItemText(IDC_OPT_MAX_CONN, sVal);
	//pW->options.SetOption(OPT_MAX_CONNECTIONS, String((LPCTSTR)sVal));

	//CButton* aChk = (CButton*)GetDlgItem(IDC_OPT_USE_SSL);
	//if(aChk->GetCheck() == BST_CHECKED){
	//	sVal = "1";
	//}else{
	//	sVal = "0";
	//}
	//pW->options.SetOption(OPT_USE_SSL, String((LPCTSTR)sVal));

	CButton* aChk = (CButton*)GetDlgItem(IDC_OPT_HIDE_AGENT);
	if(aChk->GetCheck() == BST_CHECKED){
		sVal = "1";
	}else{
		sVal = "0";
	}
	pW->options.SetOption(OPT_HIDE_AGENT, String((LPCTSTR)sVal));

	//GetDlgItemText(IDC_OPT_SSL_KEY, sVal);
	//pW->options.SetOption(OPT_SSL_KEY, String((LPCTSTR)sVal));

	aChk = (CButton*)GetDlgItem(IDC_OPT_SAVE_LOG);
	if(aChk->GetCheck() == BST_CHECKED){
		sVal = "1";
	}else{
		sVal = "0";
	}
	pW->options.SetOption(OPT_SAVE_LOG, String((LPCTSTR)sVal));

	GetDlgItemText(IDC_OPT_EVT_LOG, sVal);
	pW->options.SetOption(OPT_SAVE_LOG_FILE, String((LPCTSTR)sVal));

	if(!pW->options.SaveToFile(OPTIONS_FILE)){
		MessageBox("Could not load options file 'agent_options.dat'.", ERR_TITLE, MB_OK);
	}

	return 0;

}

void COptionsDlg::OnBnClickedOk()
{
	SaveOptions();
	OnOK();
}

void COptionsDlg::OnBnClickedOptSaveLog()
{
	CButton* aChk = (CButton*)GetDlgItem(IDC_OPT_SAVE_LOG);
	CEdit* aEdt = (CEdit*)GetDlgItem(IDC_OPT_EVT_LOG);

	if(aChk->GetCheck() == BST_CHECKED){
		aEdt->EnableWindow(TRUE);
	}else{
		aEdt->EnableWindow(FALSE);
	}
}

//void COptionsDlg::OnBnClickedOptSaveLog2()
//{
  // TODO: Add your control notification handler code here
//}
